package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.dto.marketing.MarketingLinkCreateDTO;
import com.pactera.madp.cp.api.dto.marketing.MarketingLinkUpdateDTO;
import com.pactera.madp.cp.api.entity.marketing.MarketingLinkEntity;
import com.pactera.madp.cp.api.vo.marketing.MarketingLinkVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.marketing.MarketingLinkMapper;
import com.pactera.madp.cp.service.MarketingHotService;
import com.pactera.madp.cp.service.MarketingLinkService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (McMarketingLink)表服务实现类
 *
 * @author yushen.qi
 * @since 2021-05-24 18:26:00
 */
@Service
public class MarketingLinkServiceImpl extends ServiceImpl<MarketingLinkMapper, MarketingLinkEntity> implements MarketingLinkService {

    @Resource
    private MarketingHotService marketingHotService;

    @Override
    public List selectAllOrName(String title, Integer classification) {
        int corpId = CorpContextHolder.getCorpId();
        if(null==title){
            return list(Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getCorpId, corpId).eq(MarketingLinkEntity::getClassification,classification)).stream().map(entity ->
            {
                MarketingLinkVO MarketingLinkVO = new MarketingLinkVO();
                BeanUtil.copyProperties(entity, MarketingLinkVO);
                return MarketingLinkVO;
            }).collect(Collectors.toList());
        }
        return list(Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getCorpId, corpId).like(MarketingLinkEntity::getTitle,title).eq(MarketingLinkEntity::getClassification,classification)).stream().map(entity ->
        {
            MarketingLinkVO MarketingLinkVO = new MarketingLinkVO();
            BeanUtil.copyProperties(entity, MarketingLinkVO);
            return MarketingLinkVO;
        }).collect(Collectors.toList());
    }

    @Override
    public IPage selectAllByPageOrName(Page page, String title, Integer classification) {
        Integer corpId = CorpContextHolder.getCorpId();
        QueryWrapper<MarketingLinkEntity> wrapper = new QueryWrapper<>();
        if(null==title){
            wrapper.eq("corp_id",corpId).eq("marketing_state",2).eq("classification",classification);

        }else {
            wrapper.like("title",title).eq("marketing_state",2).eq("corp_id",corpId).eq("classification",classification);
        }
        IPage iPage = baseMapper.selectPage(page, wrapper);
        Object collect = iPage.getRecords().stream().map(entity -> {
            MarketingLinkVO marketingLinkVO = new MarketingLinkVO();
            BeanUtil.copyProperties(entity, marketingLinkVO);
            return marketingLinkVO;
        }).collect(Collectors.toList());
        iPage.setRecords((List) collect);
        return iPage;
    }

    @Override
    public Map<String,Integer> insert(MarketingLinkCreateDTO dto) {
        int corpId = CorpContextHolder.getCorpId();
        int tenantId = AccountService.getTenantId();
        MarketingLinkEntity linkEntity = new MarketingLinkEntity();
        BeanUtil.copyProperties(dto,linkEntity);
        linkEntity.setCreateTime(LocalDateTime.now());
        linkEntity.setUpdateTime(LocalDateTime.now());
        linkEntity.setCorpId(corpId);
        linkEntity.setHotUuid("a");
        linkEntity.setRecState(2);
       // linkEntity.setTenantId(tenantId);
        linkEntity.setMarketingState(1);
        save(linkEntity);
        MarketingLinkEntity one = getOne(Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getCorpId, corpId).orderByDesc(MarketingLinkEntity::getId).last("limit 1"));
        HashMap<String, Integer> hashMap = new HashMap<>(1);
        hashMap.put("id",one.getId());
        return hashMap;
    }

    @Override
    public boolean update(MarketingLinkUpdateDTO marketingLinkUpdateDto) {
        MarketingLinkEntity linkEntity = new MarketingLinkEntity();
        BeanUtil.copyProperties(marketingLinkUpdateDto,linkEntity);
        linkEntity.setUpdateTime(LocalDateTime.now());
        linkEntity.setMarketingState(3);
        linkEntity.setHotUuid(null);
        linkEntity.setRecState(2);
        return update(linkEntity,Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getId,linkEntity.getId()));
    }

    @Override
    public R<Boolean> remove(Integer id) {

        MarketingLinkEntity one = getOne(Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getId, id));
        if (one.getMarketingState()==2){
            return R.failed("该素材已被发布，无法删除！");
        }
        if(one.getRecState()==1){
            return R.failed("该素材已被推荐，无法删除！");
        }
//        MarketingHotEntity hotEntity = marketingHotService.getOne(Wrappers.<MarketingHotEntity>lambdaQuery()
//                .eq(MarketingHotEntity::getCorpId,CorpContextHolder.getCorpId())
//                .orderByDesc(MarketingHotEntity::getId).last("limit 1"));
//        if(null!=hotEntity){
//            if(one.getHotUuid().equals(hotEntity.getHotUuid())){
//                return R.failed("该素材已在热榜发布，无法删除！");
//            }
//        }
        return remove(Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getId,id))?R.ok():R.failed("删除失败！");
    }


}
